package com.tanhua.api.impl.mongo;

import com.tanhua.api.mongo.InteractionApi;
import com.tanhua.domain.mongo.Interaction;
import com.tanhua.domain.mongo.Video;
import com.tanhua.domain.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service
public class InteractionApiImpl implements InteractionApi {
    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public Video findById(String videoId) {
        return mongoTemplate.findById(new ObjectId(videoId),Video.class);
    }

    @Override
    public Long shiping(Interaction interaction) {

        interaction.setCreated(System.currentTimeMillis());
        Video video = mongoTemplate.findById(interaction.getVideoId(), Video.class);

        interaction.setVideoUserId(video.getUserId());

        mongoTemplate.insert(interaction,"video_interaction");
        Query query = new Query(Criteria.where("id").is(interaction.getVideoId()));
        Update update = new Update();
        update.inc(interaction.getCol(),1); //点赞 评论 喜欢
        mongoTemplate.upsert(query,update,Video.class);
        Query qr = new Query(Criteria.where("intType").is(interaction.getIntType())
                .and("interactionType").is(interaction.getInteractionType()).and("videoId").is(interaction.getVideoId()));
        long count = mongoTemplate.count(qr, "video_interaction");
        return count;
    }

    @Override
    public Long shiping2(Interaction interaction) {
        Query removeQuery = new Query(
                Criteria.where("videoId").is(interaction.getVideoId())
                        .and("interactionType").is(interaction.getInteractionType())
                        .and("userId").is(interaction.getUserId()).and("intType").is(interaction.getIntType()));
        mongoTemplate.remove(removeQuery,"video_interaction");
        Video video = mongoTemplate.findById(interaction.getVideoId(), Video.class);
        video.setLikeCount(video.getLikeCount()-1);
        mongoTemplate.save(video);
        Query qr = new Query(Criteria.where("intType").is(interaction.getIntType())
                .and("interactionType").is(interaction.getInteractionType()).and("videoId").is(interaction.getVideoId()));
        long count = mongoTemplate.count(qr, "video_interaction");
        return count;
    }

    @Override
        public PageBeanVo findInteraction(String commentsId,Integer page, Integer pagesize) {
        Query query = new Query(Criteria.where("videoId").is(new ObjectId(commentsId)).and("interactionType").is(2).and("intType").is(2));
        query.with(new Sort(Sort.Direction.DESC,"created"));
        query.skip((page-1)*pagesize).limit(pagesize);
        List<Interaction> list = mongoTemplate.find(query, Interaction.class);
        long count = mongoTemplate.count(query, Interaction.class);
        return new PageBeanVo(page,pagesize,count,list);
    }
}
